package com.github.cdisk.xtrans.ui.widget;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

public class MoreButtonText extends Composite {
	private Text text;
	private Button button;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public MoreButtonText(Composite parent, TextDialog dialog, int style) {
		super(parent, style);
		setLayout(new FormLayout());
		
		text = new Text(this, SWT.BORDER);
		FormData fd_text = new FormData();
		fd_text.left = new FormAttachment(0);
		fd_text.top = new FormAttachment(0);
		text.setLayoutData(fd_text);
		
		button = new Button(this, SWT.FLAT);
		fd_text.right = new FormAttachment(button);
		fd_text.bottom = new FormAttachment(button, 0, SWT.BOTTOM);
		FormData fd_button = new FormData();
		fd_button.top = new FormAttachment(0);
		fd_button.bottom = new FormAttachment(100);
		fd_button.right = new FormAttachment(100);
		button.setLayoutData(fd_button);
		button.setText("...");
		button.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent event) {
				try {
					String result = dialog.open(String.valueOf(getData()), text.getText());
					if (result != null) {
						text.setText(result);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});

	}

	@Override
	protected void checkSubclass() {
	}

	public void addModifyListener(ModifyListener modifyListener) {
		text.addModifyListener(modifyListener);
	}

	public void selectAll() {
		text.selectAll();
	}

	public void setText(String text) {
		this.text.setText(text);
	}

	public String getText() {
		return this.text.getText();
	}
}
